//
//  RootTableViewController.m
//  音乐播放器
//
//  Created by xalo on 16/1/4.
//  Copyright © 2016年 zhiying.com. All rights reserved.
//

#import "RootTableViewController.h"
#import "HTTPHelper.h"
#import "RootTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "PlayViewController.h"
#import "musicPlayerCenter.h"
@interface RootTableViewController ()

@property(nonatomic,retain)NSMutableArray *dataArray;

@end

@implementation RootTableViewController
{
    //记录正在播放的下标
    
    
    
    NSInteger _currentIndex;
    //播放控制器对象
    PlayViewController *_playVC;
}
- (NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    //监听上下一曲的通知
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(nextMusic) name:@"next" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector( beforeMusic:) name:@"before" object:nil];
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector( nextMusic) name:AVPlayerItemDidPlayToEndTimeNotification object:nil];
    
    
[HTTPHelper loadDataWithUrlString:@"http://project.lanou3g.com/teacher/UIAPI/MusicInfoList.plist" callBack:^(id source) {
    NSLog(@"%@",source);
    
    //将获取的数据转换为model类
    for (NSDictionary *tempDic in source) {
        
        [self.dataArray addObject:[MusicModel setModelWithDictionary:tempDic]];
        
        
    }
    
    
   // NSLog(@"---%@",self.dataArray);
    [self.tableView reloadData];
}];

  
    NSLog(@"%f",[UIScreen mainScreen].bounds.size.width);
   
}
//下一曲的执行方法
- (void)nextMusic{
    if (_currentIndex == self.dataArray.count - 1) {
        _currentIndex =0;
    }else{
       _currentIndex +=1;
    }
    
    //取出下一个model
    MusicModel *nextMode = self.dataArray[_currentIndex];
    //播放
    [[musicPlayerCenter defaultCenter]playWithUrlString:nextMode.mp3Url];
    [_playVC setPlayInfoWithMode:nextMode];
}
//上一曲的执行方法
- (void)beforeMusic:(NSNotification *)message{
    if (_currentIndex == 0) {
        _currentIndex =self.dataArray.count -1;
    }else{
         _currentIndex -=1;
    }
   
    //取出上一曲的model
    //从通知中获取到视图控制器对象
    PlayViewController *playVC = message.object;
    MusicModel *beforeMode = self.dataArray[_currentIndex];
    [[musicPlayerCenter defaultCenter]playWithUrlString:beforeMode.mp3Url];
    //调用刷新方法
    [playVC setPlayInfoWithMode:beforeMode];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {

    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    RootTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    MusicModel *model = self.dataArray[indexPath.row];
    [cell.MusicImage sd_setImageWithURL:[NSURL URLWithString:model.picUrl]];
    cell.contentLabel.text = model.name;
    
    
    return cell;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    //取出对应的model
    MusicModel *model = self.dataArray[indexPath.row];
    //开始播放
    [[musicPlayerCenter defaultCenter]playWithUrlString:model.mp3Url];
    //记录被点的下标
    _currentIndex = indexPath.row;
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    //sender就是被点击的控件，无论它使什么类型
    //从segue中获取目标控制器（将要指向的控制器） 记录下这个控制器的地址
    _playVC = [segue destinationViewController];
    
   // playVC.musicModel = self.dataArray [[self.tableView indexPathForCell:sender].row ];
    
    //通过sender从tableView中获取下标（此时的sender就是被点击的cell）
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    //取出对应的model
    _playVC.musicModel = self.dataArray[indexPath.row];
    




}


@end
